// pages/session/session.js
var util = require('../../utils/util.js')
var http = require('../../utils/http.js')

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sessionList: [
      {
        sessionTime: '13点场',
        sessionPeople: 102363,
        sessionOrderImg: '/redbagCompetition/images/order.png',
        selected: true,
      },
      {
        sessionTime: '19点场',
        sessionPeople: 26541,
        sessionOrderImg: '/redbagCompetition/images/have_order.png',
        selected: true,
      },
      {
        sessionTime: '21点场',
        sessionPeople: 31540,
        sessionOrderImg: '/redbagCompetition/images/order.png',
        selected: true,
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userMoney = app.globalData.money;
    var haveWinNum = wx.getStorageSync('totalNum');
    that.setData({
      userMoney,
      haveWinNum
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getSession();
    var now = new Date();
    var hours = now.getHours();
    if(hours <= 13) {
      that.setData({
        beginTime: 13
      })
    } else if(hours > 13  && hours <= 19) {
      that.setData({
        beginTime: 19
      })
    } else {
      that.setData({
        beginTime: 21
      })
    }
  },
  //获取场次信息
  getSession: function() {
    var that = this;
    http.redbagSession().then( (res) => {
      var gameList = res.game;
      that.setData({
        gameList,
      })
      console.log(gameList);
    })
  },
  // 预约
  order: function(e) {
    var index = e.currentTarget.dataset.index;
    var that = this;
    util.log(index);
    var gameList = that.data.gameList;
    var gameId = gameList[index].id;
    const data = {
      gameId: gameId
    }
    http.orderGame(data).then( res => {
      that.getSession();
    })
    
  },
  // 返回
  back:function() {
    wx.navigateBack({
      delta: 1,
    })
  },
  // 立即参赛
  playNow: function() {
    // wx.navigateTo({
    //   url: '/pages/countdown/countdown'
    // })

    var that = this;
    var gameList = that.data.gameList;
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var totalNum = wx.getStorageSync('totalNum');
    if( totalNum >= 50) {
      for( var i = 0; i < gameList.length; i++) {
        if( hours == gameList[i].beginTime && minutes <= gameList[i].endTime) {
          if(gameList[i].type == 0) {
            wx.showModal({
              title: '提示',
              content: '请先预约比赛',
            })
          } else if( gameList[i].type == 1) {
            var gameId = gameList[i].id;
            wx.navigateTo({
              url: '/pages/countdown/countdown?gameId=' +gameId,
            })
          }
        } else if(hours == gameList[i].beginTime && minutes > gameList[i].endTime) {
          wx.showModal({
            title: '提示',
            content: '红包已瓜分完毕，下一场要抓紧哦'
          })
        }
      }
      if( hours != 13 && hours != 19 && hours != 21) {
          wx.showModal({
            title: '提示',
            content: '未到时间',
            showCancel: false
          })
      }
    } else {
      wx.showModal({
        content: '获胜50场可开启',
        showCancel: false
      })
    }
},
  // 红包赛记录
  record: function() {
    wx.navigateTo({
      url: '/pages/record/record'
    })
  },
  //提现跳转界面
  withdraw: function () {
    wx.navigateTo({
      url: '/pages/withdraw/withdraw'
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})